//
//  ProgramIntroducedViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/8.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "ProgramIntroducedViewController.h"

#import "ProgramIntroducedTableViewCell.h"
#import "ProgramIntroducedPartnersTableViewCell.h"

#import "ProgramModel.h"
#import "RFMImageView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MerchantModel.h"
#import "UtilsCell.h"


@interface ProgramIntroducedViewController () <UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>{
    NSArray *mallArray;
}
@property (nonatomic , weak) IBOutlet UITableView *tableview;



@property (nonatomic , weak) IBOutlet UIView *playerView;
@property (nonatomic , weak) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UIButton *playButton;


@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic , strong) RFMImageViewItem *imageItem;
@property (nonatomic , strong) NSMutableArray *partnersDatas;//商家信息
@end

@implementation ProgramIntroducedViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchMallList];
    [self initViewData];
    
}

-(void)fetchMallList{
    [[MerchantModel sharedModel] fetchMerchantsListWithColId:_item.programId key:@"" typeId:@"" currentPage:1 pageSize:1000 result:^(NSArray *list, NSString *msg) {
        mallArray = [NSArray arrayWithArray:list];
        [self.collectionView reloadData];
    }];
}


-(void)initViewData{
    if (_item.MEURL == nil || [_item.MEURL isEqualToString:@""]) {
        self.playButton.hidden = YES;
    }else{
        self.playButton.hidden = NO;
    }
    
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:_item.PICURL]];
    self.titleLabel.text = _item.ACTDESC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProgramIntroducedTableViewCell *cell = (ProgramIntroducedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ProgramIntroducedTableViewCell"];
    if (!cell) {
        cell = _LoadXib(ProgramIntroducedTableViewCell,nil,nil);
    }

    [cell.timeLabel setText:_F(@"播出时间: %@ - %@",[[_item.STARTTIME componentsSeparatedByString:@" "] lastObject],[[_item.ENDTIME componentsSeparatedByString:@" "] lastObject])];
    [cell.introducedTextView setText:_item.ACTDETILDESC];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProgramIntroducedTableViewCell *cell = (ProgramIntroducedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ProgramIntroducedTableViewCell"];
    if (!cell) {
        cell = _LoadXib(ProgramIntroducedTableViewCell,nil,nil);
    }
    
    [cell.timeLabel setText:_F(@"播出时间:%@-%@",_item.STARTTIME,_item.ENDTIME)];
    [cell.introducedTextView setText:_item.ACTDETILDESC];
    
    [cell layoutIfNeeded];
    
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return height + 1;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (IBAction)playAction:(id)sender {
    NSURL *movieUrl = [NSURL URLWithString:_item.MEURL];
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
    player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    
    [self presentMoviePlayerViewControllerAnimated:player];
}








-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return mallArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MallCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MallCollectionCell" forIndexPath:indexPath];
    MerchantItem *item = mallArray[indexPath.row];
    [cell.cellImgeView sd_setImageWithURL:[NSURL URLWithString:item.MERCHANT_IMG_URL]];
    cell.titleLabel.text = item.MERCHANT_NAME;
    cell.titleLabel.text = item.MERCHANT_DESC;
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
}


@end
